package com.bro.person;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;

/**
 * @author swallow
 * Created by swallow on 2018/12/4.<br>
 */
public class HDFSApp {

    FileSystem fileSystem = null;
    Configuration configuration = null;

    public static final String HDFS_PATH = "hdfs://47.95.112.95:8020";

    @Before
    public void setUp() throws Exception{
        configuration = new Configuration();
        configuration.set("dfs.client.use.datanode.hostname", "true");
        fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
        System.out.println("HDFSAPP.setUp");
    }

    /**
     * 创建目录
     * @throws Exception
     */
    @Test
    public void mkdir() throws Exception{
        boolean mkdirs = fileSystem.mkdirs(new Path("/hadoopapi/test/cc"));
        System.out.println(mkdirs);
    }

    /**
     * 展现目录
     * @throws Exception
     */
    @Test
    public void showDir() throws Exception{
        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
        for (FileStatus fileStatus :
                fileStatuses) {
            String fileType = fileStatus.isDirectory() ? "文件夹" : "文件";
            short replication = fileStatus.getReplication();
            long len = fileStatus.getLen();
            Path path = fileStatus.getPath();
            System.out.println(fileType+" "+replication+" "+len+" "+path.getName());
        }
    }

    /**
     * 上传大文件
     * @throws Exception
     */
    @Test
    public void copyFromLocal() throws Exception{
        Path path1 = new Path("/Users/jdd/learn/build/hadoop环境步骤.txt");
        Path path12= new Path("/hadoop环境步骤.txt");
        fileSystem.copyFromLocalFile(path1,path12);
    }

    /**
     * 上传大文件
     * @throws Exception
     */
    @Test
    public void copyFromLocalWithProgress() throws Exception{
        InputStream in = new BufferedInputStream(new FileInputStream(new File("/Users/jdd/Downloads/jenkins-2.152.pkg")));
        FSDataOutputStream out = fileSystem.create(new Path("/jenkins.pkg"), new Progressable() {
            public void progress() {
                System.out.println(".");
            }
        });
        IOUtils.copyBytes(in,out,4086);
    }


    @After
    public void tearDown() throws Exception{
        configuration = null;
        fileSystem = null;
        System.out.println();
        System.out.println("HDFSAPP.tearDown");
    }

}
